// var request = require('request');

const getCookide = require('./mock');
const oilModel = require('./db/schema');
const hoilday = require('./hoilday');
// return;
// console.log(Mock.getDatas());
// exports.getDatas = async () => { 
//     // console.log('123123', dataList, currentValue);
// }

// console.log(hoilday);
// let sl = [];
// for (let i = 0; i<hoilday.year2021.length;i++) {
//     // console.log(hoilday[i].type)
//     // console.log(hoilday[i].dayStr, hoilday[i].type)
//     const key = hoilday.year2021[i].dayStr.substr(4);
//     sl.push({
//         [key]: hoilday.year2021[i].type,
//     })
// }
// console.log(sl);

// return;

// const currentOilList = Mock.getDatas();
global.lastValue = '';
const insert = async function() {
    const cookie = await getCookide.getCookide();
    const dataStr = cookie.body;
    // console.log('123123123', dataStr)
    const currentOilList = dataStr.split(',');
    // console.log(currentOilList[31]);
    // return dataList;
    const tempOil = new oilModel({
        todayValue: currentOilList[1],
        yesterdayValue: currentOilList[2],
        currentValue: currentOilList[3],
        highest: currentOilList[4],
        lowest: currentOilList[5],
        bidPrice: currentOilList[6],
        AuctionPrice: currentOilList[7],
        tradeNum: currentOilList[8],
        tradeTotal: currentOilList[9],
        oneShareBuy: currentOilList[10],
        oneShareBuyPrice: currentOilList[11],
        twoShareBuy: currentOilList[12],
        twoShareBuyPrice: currentOilList[13],
        threeShareBuy: currentOilList[14],
        threeShareBuyPrice: currentOilList[15],
        fourShareBuy: currentOilList[16],
        fourShareBuyPrice: currentOilList[17],
        fiveShareBuy: currentOilList[18],
        fiveShareBuyPrice: currentOilList[19],
        oneShareSales: currentOilList[20],
        oneShareSalesPrice: currentOilList[21],
        twoShareSales: currentOilList[22],
        twoShareSalesPrice: currentOilList[23],
        threeShareSales: currentOilList[24],
        threeShareSalesPrice: currentOilList[25],
        fourShareSales: currentOilList[26],
        fourShareSalesPrice: currentOilList[27],
        fiveShareSales: currentOilList[28],
        fiveShareSalesPrice: currentOilList[29],
        date: currentOilList[30],
        time: currentOilList[31],
    })
    if (global.lastValue !== currentOilList[31]) {
        tempOil.save((err,data)=>{
            if(err){
                console.log(err, "存入失败，请检查数据类型");  //失败的回调
            }else{
                global.lastValue = currentOilList[31];
                console.log(data.currentValue, data.time);//成功的回调
            }
        })

    }
}
setInterval(()=>{
    insert();
}, 1000);

// const mongoose  = require('mongoose');
// const db = 'mongodb://localhost:27017/echart_oil_db';
// mongoose.connect(db);
// mongoose.connection.once('open', ()=>{
//     console.log('success')
// })

// http://hq.sinajs.cn/list=sh600519

// request({
//     url: url,
//     method: 'GET',
//     headers: {
//         'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
//     }
// }, function(err, response) {
//     console.log('123123', response.headers['set-cookie']);
// })
var hq_str_sh600519="贵州茅台,2055.080,2058.880,2042.100,2079.880,2037.020,2042.000,2042.100,2424823,4976601458.000,2600,2042.000,1500,2041.900,300,2041.270,100,2041.200,400,2041.100,102,2042.100,100,2042.140,300,2042.150,300,2042.200,100,2042.270,2021-05-21,15:00:00,00,";
// 贵州茅台,
// 2055.080, // 今日开盘价 todayValue
// 2058.880, // 昨日收盘价 yesterdayValue
// 2042.100, // 当前价格 currentValue
// 2079.880, // 今日最高价 highest
// 2037.020, // 今日最低价 lowest
// 2042.000, // 竞买价，即“买一”报价  bidPrice
// 2042.100, // 竞卖价，即“卖一”报价； AuctionPrice
// 2424823, // 成交的股票数，由于股票交易以一百股为基本单位，所以在使用时，通常把该值除以一百；tradeNum
// 4976601458.000, // 成交金额，单位为“元”，为了一目了然，通常以“万元”为成交金额的单位，所以通常把该值除以一万； tradeTotal
// 2600, // “买一”申请2600股，即26手； oneShareBuy
// 2042.000, // “买一”报价； oneShareBuyPrice
// 1500, // “买二”  twoShareBuy
// 2041.900, // “买二”  twoShareBuyPrice
// 300, // “买三” threeShareBuy
// 2041.270, // “买三”  threeShareBuyPrice
// 100, // “买四” fourShareBuy
// 2041.200, // “买四” fourShareBuyPrice
// 400, // “买五” fiveShareBuy
// 2041.100, // “买五” fiveShareBuyPrice
// 102, // 卖一 oneShareSales
// 2042.100, // 卖一 oneShareSalesPrice
// 100, // 卖二 twoShareSales
// 2042.140, // 卖二 twoShareSalesPrice
// 300, // 卖三 threeShareSales
// 2042.150, // 卖三 threeShareSalesPrice
// 300, // 卖四 fourShareSales
// 2042.200, // 卖四 fourShareSalesPrice
// 100, // 卖五 fiveShareSales
// 2042.270, // 卖五 分别为“卖二”至“卖四的情况” fiveShareSalesPrice
// 2021-05-21, // 日期；date
// 15:00:00, // 时间 time
// 00, 
// const getCookide = function() {
//     return new Promise( (resolve)=> {
//         request({
//             url: 'http://hq.sinajs.cn/list=sh600519',
//             method: 'GET',
//             headers: {
//                 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
//             }
//         }, (err, response) => {
//             resolve(response);
//         })
//     })
// }
// const getDatas = async () => {
//     const cookie = await getCookide();
//     const dataStr = cookie.body;
//     const dataList = dataStr.split(',');
//     const currentValue = dataList[3];
//     console.log('123123', dataList, currentValue);
// }

// db.createCollection("oil_collection", { capped : true, autoIndexId : true, size : 4194304000, max : 1000000 } ) 创建数据库时的基本信息
// console.log(getDatas());
console.log('hello world');
// const getCookie = function() {
//     return new Promise( (resolve)=> {
//         request({
//             url: 'https://m.cn.investing.com/commodities/crude-oil',
//             method: 'GET',
//             headers: {
//                 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
//             }
//         }, (err, response) => {
//             resolve(response.headers['set-cookie']);
//         })
//     })
// }
// const getData = async () => {
//     const cookie = await getCookie();
//     console.log('123123', cookie);
//     const newData = {
//         'volume_series': 'yes',
//         'events': 'no',
//         'candle_count': 40,
//         'pair_interval': '900',
//         'chart_type': 'candlestick',
//         'pair_id_for_news': '0',
//         'pair_id': '8849',
//         'symbol': 'WTI%25E5%258E%259F%25E6%25B2%25B9%25E6%259C%259F%25E8%25B4%25A7',
//         'showECEvents': 'false',
//     }
//     request({
//         url: 'https://m.cn.investing.com/commodities/crude-oil',
//         method: 'GET',
//         headers: {
//             "Accept": "application/json, text/javascript, */*; q=0.01",
//             "Accept-Encoding": "gzip, deflate, br",
//             "Accept-Language": "zh-CN,zh;q=0.9",
//             "Connection": "keep-alive",
//             'Cookie': cookie,
//             "Host": "m.cn.investing.com",
//             "Referer": "https://m.cn.investing.com/commodities/crude-oil",
//             "Sec-Fetch-Dest": "empty",
//             "Sec-Fetch-Mode": "cors",
//             "Sec-Fetch-Site": "same-origin",
//             "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36",
//             "X-Requested-With": "XMLHttpRequest",
//         },
//         qs: newData
//     }, (err, response) => {
//         console.log(response.body)
//     })
//     // headers = {
//     //     "Accept": "application/json, text/javascript, */*; q=0.01",
//     //     "Accept-Encoding": "gzip, deflate, br",
//     //     "Accept-Language": "zh-CN,zh;q=0.9",
//     //     "Connection": "keep-alive",
//     //     "Cookie": "{}".format(header),
//     //     "Host": "m.cn.investing.com",
//     //     "Referer": "https://m.cn.investing.com/commodities/crude-oil",
//     //     "Sec-Fetch-Dest": "empty",
//     //     "Sec-Fetch-Mode": "cors",
//     //     "Sec-Fetch-Site": "same-origin",
//     //     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36",
//     //     "X-Requested-With": "XMLHttpRequest",
//     // }
//     // result = requests.get('https://m.cn.investing.com/instrument/services/getChart', params=newData,  headers=headers, timeout=10)
// }
